package com.up360.rapidcalc.aerolite.util
{
    import flash.display.DisplayObject;
    import flash.display.DisplayObjectContainer;
    import flash.display.Sprite;

    /**
     * @author Tunie
     * @date   May 9, 2013
     * 层管理对象，一般分以下几层
     * 1.背景层，放游戏背影
     * 2.游戏层，放游戏元素
     * 3.前建议层，放前景等
     */
    public class LayerManager
    {
        public static const BACKGROUND:String="_bgLayer";
        public static const GAME:String="_gameLayer";
        public static const OUTLOOK:String="_outLookLayer";
        /**
         *父容器，需要层管理的对象 
         */
        private var _target:Sprite;
        private var _bgLayer:Sprite;
        private var _gameLayer:Sprite;
        private var _outLookLayer:Sprite;
        
        public function LayerManager(target:Sprite)
        {
            _target=target;
            setup();
        }
        
        private function setup():void
        {
            _bgLayer=new Sprite();
            _gameLayer=new Sprite();
            _outLookLayer=new Sprite();
            
            _bgLayer.name="_bgLayer";
            _gameLayer.name="_gameLayer";
            _outLookLayer.name="_outLookLayer";
            
            _outLookLayer.mouseEnabled=false;
            
            _target.addChild(_bgLayer);
            _target.addChild(_gameLayer);
            _target.addChild(_outLookLayer);
        }
        
        /**
         * 添加显示元件至显示列表 
         * @param obj
         * @param lays 指定层
         * 
         */
        public function add(obj:DisplayObject,layer:String="_bgLayer"):void
        {
            var b_layer:Sprite=_target.getChildByName(layer) as Sprite;
            b_layer.addChild(obj);
        }
        
        public function remove(obj:DisplayObject):void
        {
            var b_layer:Sprite=getLayer(obj);
            if(b_layer)
            {
                b_layer.removeChild(obj);
            }
        }
        
        /**
         *将指定显示元件置于所在层顶层 
         * @param obj
         */
        public function top(obj:DisplayObject):void
        {
            var b_layer:Sprite=getLayer(obj);
            if(b_layer)
            {
                b_layer.setChildIndex(obj,b_layer.numChildren-1);
            }
        }
        
        /**
         * 将指定显示元件置于所在层底层 
         * @param obj
         * 
         */
        public function bottom(obj:DisplayObject):void
        {
            var b_layer:Sprite=getLayer(obj);
            if(b_layer)
            {
                b_layer.setChildIndex(obj,0);
            }
        }
        
        /**
         * 取得所在层
         * @param obj
         * 
         */
        public function getLayer(obj:DisplayObject):Sprite
        {
           var b_count:int=0;
           while(_target.numChildren>b_count)
           {
               var b_layer:Sprite=_target.getChildAt(b_count) as Sprite;
               if(b_layer.contains(obj))
               {
//                   trace(b_layer.name);
                   return b_layer;
               }
               b_count++;
           }
           return null;
        }
        
        /**
         *取得所在层的所在索引 
         * @return 
         */
        public function getIndex(obj:DisplayObject):int
        {
            var b_layer:Sprite=getLayer(obj);
            return b_layer.getChildIndex(obj);
        }
        
        /**
         *清空指定层 
         * @param layer
         * 
         */
        public function clearLayer(layer:String="_bgLayer"):void
        {
            var b_layer:Sprite=_target.getChildByName(layer) as Sprite;
            if(b_layer)
            {
                while(b_layer.numChildren>0)
                {
                    b_layer.removeChildAt(0);
                }
            }
            else
            {
                trace(_target,"不存在该层");
            }
        }
        /**
         * 清空所有层
         */
        public function clear():void
        {
            var b_count:int=0;
            while(_target.numChildren>b_count)
            {
                var b_layer:Sprite=_target.getChildAt(b_count) as Sprite;
                while(b_layer.numChildren>0)
                {
                    b_layer.removeChildAt(0);
                }
                b_count++;
            }
        }
    }
}